const {login} = require("../controller/user");
const {SuccessModel,ErrorModel} = require("../model/resmodel");
const {set} = require("../db/redis")
//获取cookie 过期时间
const getCookieExpires = ()=>{
    let d = new Date();
    d.setTime(d.getTime()+(24*60*60*1000));
    // console.log(d.toGTMstring())
    return (d.toGMTString());
}
const handelUserRouter = (req,res)=>{
    const method = req.method;
    //登陆
    if(method === "POST" && req.path ==="/api/user/login"){
        const {username,password} = req.body;
        const result = login(username,password);
        return result.then(data=>{
            if(data.username){
                //设置sessin
                req.session.username = data.username;
                req.session.realname = data.realname;
                //同步到redis
                set(req.sessionId,req.session);
                return new SuccessModel("登陆成功");
            }else{
                return new ErrorModel("登陆失败");
            }
        })
       
    }
    //登陆验证的测试
    // if(method === "GET" && req.path === "/api/user/login-test"){
    //     console.log('erci:',req.session)
    //     if(req.session.username){
    //         return Promise.resolve(new SuccessModel({
    //             session: req.session
    //         })) ;
    //     }else{
    //         return Promise.resolve(new ErrorModel("登陆失败")) ;
    //     }
    // }

}
module.exports = {
    handelUserRouter,
    getCookieExpires
};